use super::Result;
use reqwest::header::HeaderMap;
use serde_json::Value as JsonValue;
use snafu::prelude::*;

#[derive(Debug, Clone)]
pub struct HttpResponse {
    pub status: u16,
    pub headers: HeaderMap,
    pub body: bytes::Bytes,
}

impl HttpResponse {
    pub fn text(&self) -> Result<String> {
        Ok(std::str::from_utf8(&self.body)
            .whatever_context("Body is not valid UTF-8")?
            .to_string())
    }
    pub fn json(&self) -> Result<JsonValue> {
        let value = serde_json::from_slice::<JsonValue>(&self.body)
            .whatever_context("JSON is not valid JSON")?;
        Ok(value)
    }
}
